package org.example.leetCode;

import java.lang.reflect.Proxy;

interface ia {
    String getHelloName();
}

public class ShowMeBugs {
    public static void main(String[] arges) throws Exception {
        ia ia = (org.example.leetCode.ia) createObject(org.example.leetCode.ia.class.getName() + "$getHelloName=Abc");
        System.out.println(ia.getHelloName()); //方法名匹配的时候，输出“Abc”
        ia = (org.example.leetCode.ia) createObject(org.example.leetCode.ia.class.getName() + "$getTest=Bcd");
        System.out.println(ia.getHelloName()); //方法名不匹配的时候，输出null
    }

    //请实现方法createObject，接口中"getName()"方法名仅仅是个示例，不能写死判断

    public static Object createObject(String str) throws Exception {
        int i1 = str.indexOf("$");
        int i2 = str.indexOf("=");
        String className = str.substring(0, i1);
        String methodName = str.substring(i1 + 1, i2);
        String result = str.substring(i2 + 1);

        return Proxy.newProxyInstance(Class.forName(className).getClassLoader(), new Class[]{Class.forName(className)},
                ((proxy, method, args) -> {
                    if (method.getName().equals(methodName)) {
                        return result;
                    } else {
                        return null;
                    }
                })
        );
    }
}

